using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;
using SqlRunner;
using SQLRunnerLib.Util;

namespace SQLRunner.gui
{
    partial class frmMain
    {
        private void btnExecuteScripts_Click(object sender, EventArgs e)
        {
            btnExecuteScripts.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText;
        }

        private void addFolderBtn_Click(object sender, EventArgs e)
        {
            DirectoryInfo aDir = GetDefaultPath();
            if (aDir != null && aDir.Exists) fBrowser.SelectedPath = aDir.FullName;
            if (fBrowser.ShowDialog(this) != DialogResult.OK || fBrowser.SelectedPath.Length <= 0) return;
            aDir = new DirectoryInfo(fBrowser.SelectedPath);
            _log.Debug(String.Format("{0} folder was added to the list", aDir.FullName));
            scriptController.AddNewFolder(aDir);
        }

        private readonly RunnerSettings _appSettings = RunnerSettings.GetRunnerSettings();

        public DirectoryInfo GetDefaultPath()
        {
            DirectoryInfo aDir = null;
            RunnerProperties properties = _appSettings.DefaultSetProperties();
            RunnerProperty property = properties != null ? properties.FindProperty("path") : null;

            if (property != null && property.value.Length > 0)
            {
                aDir = new DirectoryInfo(property.value);
            }

            return aDir;
        }

        private void addFileBtn_Click(object sender, EventArgs e)
        {

            //DirectoryInfo aDir GetDefaultPath();
            //if (aDir.Exists) openFile.InitialDirectory = aDir.FullName;
            //openFile.Multiselect = true;
            //if (openFile.ShowDialog(this) == DialogResult.OK && openFile.FileNames.Length > 0)
            //{
            //    StringBuilder strB = new StringBuilder();
            //    foreach (string fileName in openFile.FileNames)
            //    {
            //        strB.AppendLine(fileName);
            //    }
            //    _log.DebugFormat("The following file(s) were added to the list:\n{0}", strB.ToString());
            //}
        }

        private int _treeUpdateLevel = 0;

        private void fileTreeViewer_AfterCheck(object sender, TreeViewEventArgs e)
        {
            // Only update children if we are going down
            if (_treeUpdateLevel >= 0)
            {
                // Update all the children
                foreach (TreeNode node in e.Node.Nodes)
                {
                    _treeUpdateLevel++;
                    node.Checked = e.Node.Checked;
                    scriptController.NodeChecked(node.Name, node.Checked);
                    _treeUpdateLevel--;
                }
            }

            if (_treeUpdateLevel > 0 || e.Node.Parent == null) return;

            // At this point we go upwards
            // the parent should be unchecked only if all siblings are unchecked
            Boolean parentChecked = e.Node.Checked;
            foreach (TreeNode node in e.Node.Parent.Nodes)
            {
                if (node.Checked)
                {
                    parentChecked = true;
                    break;
                }
            }
            if (parentChecked != e.Node.Parent.Checked)
            {
                // indicate that a parent is updating                
                _treeUpdateLevel--;
                e.Node.Parent.Checked = parentChecked;
                scriptController.NodeChecked(e.Node.Parent.Name, parentChecked);
                _treeUpdateLevel++;
            }
        }

        private void fileTreeViewer_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && fileTreeViewer.SelectedNode != null)
            {
                switch (e.KeyCode)
                {
                    case Keys.Down:
                        MoveNodeDown(fileTreeViewer.SelectedNode);
                        break;
                    case Keys.Up:                                                                       
                        MoveNodeUp(fileTreeViewer.SelectedNode);
                        break;
                    case Keys.Delete:
                        fileTreeViewer.SelectedNode.Remove();
                        break;

                    default:
                        return;
                }
            }
        }
    }
}
